package main

import "fmt"

func spiralOrder(matrix [][]int) []int {
	bottom := len(matrix)
	right := len(matrix[0])
	left := 0
	top := 0
	index, i, j := 0, 0, 0
	length := bottom * right
	element := make([]int, length)

	for {
		if index == length {
			break
		}
		for {
			if j < right {
				element[index] = matrix[i][j]
				index++
				j++
			}
			if j == right {
				right--
				j--
				break
			}
		}
		if index == length {
			break
		}
		for {
			if i < bottom-1 {
				i++
				element[index] = matrix[i][j]
				index++
			}
			if i == bottom-1 {
				bottom--
				break
			}
		}
		if index == length {
			break
		}
		for {
			if j > left-1 {
				j--
				element[index] = matrix[i][j]
				index++

			}
			if j == left {
				left++
				break
			}
		}
		if index == length {
			break
		}
		for {
			if i > top {
				i--
				element[index] = matrix[i][j]
				index++
			}
			if i == top+1 {
				top++
				j++
				break
			}
		}
	}

	return element
}
func main() {
	//matrix := [][]int{
	//	{1, 2, 3},
	//	{4, 5, 6},
	//	{7, 8, 9},
	//}
	matrix := [][]int{
		{1},
	}

	fmt.Println(spiralOrder(matrix))

}
